package com.itheima.appserver.controller;

import com.itheima.appserver.service.CommentsService;
import com.itheima.appserver.service.MovementService;
import com.itheima.model.mongo.Movement;
import com.itheima.model.vo.MovementsVo;
import com.itheima.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;
    @Autowired
    private CommentsService commentsService;

    //发布动态
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] images) throws IOException {
        movementService.publishMovement(movement, images);
        return ResponseEntity.ok(null);
    }

    //查看自己的动态
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize, Long userId) {
        PageResult pr = movementService.all(page, pagesize, userId);
        return ResponseEntity.ok(pr);
    }

    //查看好友动态
    @GetMapping
    public ResponseEntity getFriendMovement(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        //调用service查询分页数据
        PageResult pr = movementService.FriendMovement(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    //推荐动态
    @GetMapping("/recommend")
    public ResponseEntity getRecommendMovement(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        //调用service查询分页数据
        PageResult pr = movementService.RecommendMovemet(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/{id}")
    public ResponseEntity getOneUser(@PathVariable String id) {
        MovementsVo vo = movementService.findone(id);
        return ResponseEntity.ok(vo);
    }

    //点赞动态
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id) {
        //调用service保存
        Integer counts = commentsService.hasComment(id);
        return ResponseEntity.ok(counts);
    }

    //取消点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id) {
        //调用service保存
        Integer counts = commentsService.unhasComment(id);
        return ResponseEntity.ok(counts);
    }

    //喜欢
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.loveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    //取消喜欢
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.disloveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
}
